package zzs.generalframework.console.core.user.usersecurityweb.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zzs.generalframework.common.core.user.usersecurityweb.UserSecurityWebBO;
import zzs.generalframework.common.core.user.usersecurityweb.UserSecurityWebInfo;
import zzs.generalframework.common.core.user.usersecurityweb.UserSecurityWebPO;
import zzs.generalframework.console.common.exception.ConsoleException;
import zzs.generalframework.console.common.exception.ConsoleRollbackException;
import zzs.generalframework.console.common.model.ConsoleReturnBO;
import zzs.generalframework.console.common.model.ConsoleReturnBO.ConsoleResult;
import zzs.generalframework.console.core.user.usersecurityweb.dao.IUserSecurityWebDao;
import zzs.generalframework.console.core.user.usersecurityweb.service.IUserSecurityWebService;
import zzs.generalframework.util.core.reflect.BeanUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.user.usersecurityweb.service.impl
 * @author zzs
 * @date 2018年2月5日
 * @version 1.0
 */
@Service("zzs.generalframework.console.core.user.usersecurityweb.service.impl.userSecurityWebServiceImpl")
public class UserSecurityWebServiceImpl implements IUserSecurityWebService
{

	private Logger logger = LoggerFactory.getLogger(UserSecurityWebServiceImpl.class);
	@Autowired
	BeanUtils beanUtils;
	@Autowired
	IUserSecurityWebDao userSecurityWebDao;

	@Override
	public ConsoleReturnBO saveUserSecurityWeb(UserSecurityWebBO userSecurityWebBO)
	{
		logger.debug("====保存用户安全信息开始====");
		logger.debug("====保存用户安全信息.入参:UserSecurityWebBO=[{}]====", userSecurityWebBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		UserSecurityWebPO userSecurityWebPO = new UserSecurityWebPO();
		copyUserSecurityWebBO2UserSecurityWebPO(userSecurityWebBO, userSecurityWebPO);
		Boolean insertResult = userSecurityWebDao.insert(userSecurityWebPO);
		if (insertResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====保存用户安全信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存用户安全信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO saveUserSecurityWeb(List<UserSecurityWebBO> userSecurityWebBOList) throws ConsoleException
	{
		logger.info("====保存用户安全信息列表开始====");
		logger.debug("====保存用户安全信息列表.入参:UserSecurityWebBOList=[{}]====", userSecurityWebBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (UserSecurityWebBO itemBO : userSecurityWebBOList)
		{
			ConsoleReturnBO itemResult = saveUserSecurityWeb(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException(itemResult.getMessage());
			}
		}
		logger.debug("====保存用户安全信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存用户安全信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO getUserSecurityWeb(UserSecurityWebInfo userSecurityWebInfo)
	{
		logger.info("====获取用户安全信息开始====");
		logger.debug("====获取用户安全信息.入参:UserSecurityWebInfo=[{}]====", userSecurityWebInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<UserSecurityWebPO> userSecurityWebPOList = userSecurityWebDao.select(userSecurityWebInfo);
		if (userSecurityWebPOList == null || userSecurityWebPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else if (userSecurityWebPOList.size() == 1)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			UserSecurityWebBO userSecurityWebBO = new UserSecurityWebBO();
			copyUserSecurityWebPO2UserSecurityWebBO(userSecurityWebPOList.get(0), userSecurityWebBO);
			returnConsoleReturnBO.setReturnObj(userSecurityWebBO);
		}
		else
		{
			returnConsoleReturnBO.setMessage("记录不唯一");
			UserSecurityWebBO userSecurityWebBO = new UserSecurityWebBO();
			copyUserSecurityWebPO2UserSecurityWebBO(userSecurityWebPOList.get(0), userSecurityWebBO);
			returnConsoleReturnBO.setReturnObj(userSecurityWebBO);
		}
		logger.debug("====获取用户安全信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====获取用户安全信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO listUserSecurityWeb(UserSecurityWebInfo userSecurityWebInfo)
	{
		logger.info("====获取用户安全信息列表开始====");
		logger.debug("====获取用户安全信息列表.入参:UserSecurityWebInfo=[{}]====", userSecurityWebInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<UserSecurityWebPO> userSecurityWebPOList = userSecurityWebDao.select(userSecurityWebInfo);
		if (userSecurityWebPOList == null || userSecurityWebPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			List<UserSecurityWebBO> resultList = new ArrayList<UserSecurityWebBO>();
			for (UserSecurityWebPO itemPO : userSecurityWebPOList)
			{
				UserSecurityWebBO itemBO = new UserSecurityWebBO();
				copyUserSecurityWebPO2UserSecurityWebBO(itemPO, itemBO);
				resultList.add(itemBO);
			}
			returnConsoleReturnBO.setReturnObj(resultList);
		}
		logger.debug("====获取用户安全信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====获取用户安全信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateUserSecurityWeb(UserSecurityWebBO userSecurityWebBO)
	{
		logger.info("====更新用户安全信息开始====");
		logger.debug("====更新用户安全信息.入参:UserSecurityWebBO=[{}]====", userSecurityWebBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		UserSecurityWebPO userSecurityWebPO = new UserSecurityWebPO();
		copyUserSecurityWebBO2UserSecurityWebPO(userSecurityWebBO, userSecurityWebPO);
		Boolean updateResult = userSecurityWebDao.update(userSecurityWebPO);
		if (updateResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====更新用户安全信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新用户安全信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateUserSecurityWeb(List<UserSecurityWebBO> userSecurityWebBOList) throws ConsoleException
	{
		logger.info("====更新用户安全信息列表开始====");
		logger.debug("====更新用户安全信息列表.入参:List<UserSecurityWebBO>=[{}]====", userSecurityWebBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (UserSecurityWebBO itemBO : userSecurityWebBOList)
		{
			ConsoleReturnBO itemResult = updateUserSecurityWeb(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException(itemResult.getMessage());
			}
		}
		logger.debug("====更新用户安全信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新用户安全信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeUserSecurityWeb(UserSecurityWebBO userSecurityWebBO)
	{
		logger.info("====删除用户安全信息开始====");
		logger.debug("====删除用户安全信息.入参:UserSecurityWebBO＝[{}]======", userSecurityWebBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		UserSecurityWebPO userSecurityWebPO = new UserSecurityWebPO();
		copyUserSecurityWebBO2UserSecurityWebPO(userSecurityWebBO, userSecurityWebPO);
		Boolean deleteResult = userSecurityWebDao.delete(userSecurityWebPO);
		if (deleteResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====删除用户安全信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====删除用户安全信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeUserSecurityWeb(List<UserSecurityWebBO> userSecurityWebBOList) throws ConsoleException
	{
		logger.info("====删除用户安全信息列表开始====");
		logger.debug("====删除用户安全信息列表.入参:List<UserSecurityWebBO>＝[{}]======", userSecurityWebBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (UserSecurityWebBO itemBO : userSecurityWebBOList)
		{
			ConsoleReturnBO itemResult = removeUserSecurityWeb(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException(itemResult.getMessage());
			}
		}
		logger.debug("====删除用户安全信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====删除用户安全信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public Long countUserSecurityWeb(UserSecurityWebBO userSecurityWebBO)
	{
		logger.info("====统计用户安全信息开始====");
		logger.debug("====统计用户安全信息。入参：UserSecurityWebBO＝[{}]======", userSecurityWebBO);
		Long returnLong = new Long(0);
		UserSecurityWebPO userSecurityWebPO = new UserSecurityWebPO();
		copyUserSecurityWebBO2UserSecurityWebPO(userSecurityWebBO, userSecurityWebPO);
		returnLong = userSecurityWebDao.count(userSecurityWebPO);
		logger.debug("====统计用户安全信息.出参:Long＝[{}]======", returnLong);
		logger.info("====统计用户安全信息结束====");
		return returnLong;
	}

	private void copyUserSecurityWebBO2UserSecurityWebPO(UserSecurityWebBO userSecurityWebBO, UserSecurityWebPO userSecurityWebPO)
	{
		beanUtils.copyBeanNotNull(userSecurityWebBO, userSecurityWebPO);
		if (userSecurityWebBO.getSysID() != null)
		{
			userSecurityWebPO.setSeqID(userSecurityWebBO.getSysID());
		}
	}

	private void copyUserSecurityWebPO2UserSecurityWebBO(UserSecurityWebPO userSecurityWebPO, UserSecurityWebBO userSecurityWebBO)
	{
		beanUtils.copyBeanNotNull(userSecurityWebPO, userSecurityWebBO);
		if (userSecurityWebPO.getSeqID() != null)
		{
			userSecurityWebBO.setSysID(userSecurityWebPO.getSeqID());
		}
	}

}
